<?php

namespace app\api\logic\article;

use think\facade\Request;
use app\common\repository\article\R as ArticleRRepository;

class ArticleSet
{
    public function __construct(
        ArticleRRepository $articleRRepository
    )
    {
        $this->articleRRepository = $articleRRepository;
    }

    public function getArticles()
    {
        $type = Request::param('type');
        $sort = Request::param('sort');
        $pageIndex = Request::param('index');
        $pageSize = Request::param('size');
        if (strlen($type) == 0 || strlen($sort) == 0|| !$pageIndex || !$pageSize) {
            return ['done' => false, 'code' => config('code.common.paramsRequire')];
        }
        $order = [];
        if ($sort == 0) {
            $order[] = 'create_time desc';
            $order[] = 'view_count desc';
        }
        if ($sort == 1) {
            $order[] = 'create_time desc';
        }
        if ($sort == 2) {
            $order[] = 'view_count desc';
        }
        $pageData = $this->articleRRepository->getArticlesByTypeWithPage($type,  $order, ['index' => $pageIndex, 'size' => $pageSize]);
        return ['data' => $pageData['list'], 'page' => ['index' => $pageIndex, 'size' => $pageSize, 'count' => $pageData['count']]];
    }

    public function getByUserId()
    {
        $userId = Request::param('id');
        $pageIndex = Request::param('index');
        $pageSize = Request::param('size');
        if (!$userId || !$pageIndex || !$pageSize) {
            return ['done' => false, 'code' => config('code.common.paramsRequire')];
        }
        $pageData = $this->articleRRepository->getArticlesWithPage(['user_id' => $userId], [], ['index' => $pageIndex, 'size' => $pageSize]);
        return ['data' => $pageData['list'], 'page' => ['index' => $pageIndex, 'size' => $pageSize, 'count' => $pageData['count']]];
    }

    public function search()
    {
        $tagId = Request::param('tagId');
        $keyword = Request::param('keyword');
        $pageIndex = Request::param('index');
        $pageSize = Request::param('size');
        if (!$pageIndex || !$pageSize) {
            return ['done' => false, 'code' => config('code.common.paramsRequire')];
        }
        $pageData = $this->articleRRepository->getArticlesByTagIdAndKeywodWithPage($tagId, $keyword, ['view_count' => 'desc'], ['index' => $pageIndex, 'size' => $pageSize]);
        return ['data' => $pageData['list'], 'page' => ['index' => $pageIndex, 'size' => $pageSize, 'count' => $pageData['count']]];
    }

    public function recommend()
    {
        $tagId = Request::param('tagId');
        $keyword = Request::param('keyword');
        $pageIndex = Request::param('index');
        $pageSize = Request::param('size');
        if (!$pageIndex || !$pageSize) {
            return ['done' => false, 'code' => config('code.common.paramsRequire')];
        }
        $pageData = $this->articleRRepository->getArticlesByTagIdAndKeywodWithPage($tagId, $keyword, ['view_count' => 'desc'], ['index' => $pageIndex, 'size' => $pageSize]);
        return ['data' => $pageData['list'], 'page' => ['index' => $pageIndex, 'size' => $pageSize, 'count' => $pageData['count']]];
    }
}
